var list = []
var app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderInfo: {},
    price: '', //单价
    weightOne: '', //扣重
    weightTwo: '', //扣点

    //
    categoryStatus: false,
    objectMultiArray: [],
    multiIndex: [0, 0],
    multiArray: [],
    categoryName: '',
    categoryIdOne: '',
    categoryIdTwo: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    let data = JSON.parse(options.data)
    console.log(data)
    that.setData({ orderInfo: data })
    this.getOne()
  },
  getOne() {
    var that = this
    wx.showLoading({
      title: '拼命加载中'
    })
    wx.request({
      method: 'POST',
      url: app.preUrl + 'getListCategory',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId
      },
      success: function(res) {
        wx.hideLoading()
        let data = res.data.result
        console.log(res.data.result)
        let arrO = []
        let arrT = []
        data.forEach((item, index) => {
          arrO.push(item.name)
        })
        data[0].categoryList.forEach((item, index) => {
          arrT.push(item.name)
        })
        let newmultiArray = [arrO, arrT]
        console.log(newmultiArray)
        that.setData({
          objectMultiArray: res.data.result,
          multiArray: newmultiArray
        })
      },
      fail: function(error) {
        console.log(error)
      }
    })
  },
  //品类选择
  bindMultiPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var that = this
    that.setData({
      multiIndex: e.detail.value,
      categoryStatus: true,
      categoryIdOne: that.data.objectMultiArray[e.detail.value[0]].categoryId,
      categoryIdTwo:
        that.data.objectMultiArray[e.detail.value[0]].categoryList[
          e.detail.value[1]
        ].categoryId,
      categoryName:
        that.data.objectMultiArray[e.detail.value[0]].categoryList[
          e.detail.value[1]
        ].name
    })
    console.log(this.data.multiIndex)
    console.log(that.data.objectMultiArray[e.detail.value[0]].categoryId) //一级品类id
    console.log(
      that.data.objectMultiArray[e.detail.value[0]].categoryList[
        e.detail.value[1]
      ].categoryId
    ) //二级品类id
  },
  bindMultiPickerColumnChange: function(e) {
    var that = this
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value)

    switch (e.detail.column) {
      case 0:
        list = []
        for (var i = 0; i < that.data.objectMultiArray.length; i++) {
          if (
            that.data.objectMultiArray[i].categoryId ==
            that.data.objectMultiArray[e.detail.value].categoryId
          ) {
            for (
              let j = 0;
              j < that.data.objectMultiArray[i].categoryList.length;
              j++
            ) {
              list.push(that.data.objectMultiArray[i].categoryList[j].name)
            }
          }
        }
        that.setData({
          'multiArray[1]': list,
          'multiIndex[0]': e.detail.value,
          'multiIndex[1]': 0
        })
    }
  },

  //录入重量
  onNumberWeightOne: function(e) {
    var that = this
    that.setData({ weightOne: e.detail.value.replace(/[^\d.]/g, '') })
    that.setData({ weightOne: e.detail.value.replace(/\.{2,}/g, '.') })
    that.setData({
      weightOne: e.detail.value
        .replace('.', '$#$')
        .replace(/\./g, '')
        .replace('$#$', '.')
    })
    that.setData({
      weightOne: e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
    })
    let weight = Number(this.data.orderInfo.grossWeight) //毛重
    if (weight > 0) {
      let weightTwo = parseFloat((e.detail.value / weight) * 100).toFixed(2)
      that.setData({ weightTwo: weightTwo })
    }
  },
  //录入扣点
  onNumberWeightTwo: function(e) {
    var that = this
    that.setData({ weightTwo: e.detail.value.replace(/[^\d.]/g, '') })
    that.setData({ weightTwo: e.detail.value.replace(/\.{2,}/g, '.') })
    that.setData({
      weightTwo: e.detail.value
        .replace('.', '$#$')
        .replace(/\./g, '')
        .replace('$#$', '.')
    })
    that.setData({
      weightTwo: e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
    })
    let weight = Number(this.data.orderInfo.grossWeight) //毛重
    if (weight > 0) {
      let weightOne = parseFloat((e.detail.value * weight) / 100).toFixed(2)
      that.setData({ weightOne: weightOne })
    }
  },

  //录入单价
  onNumberPrice: function(e) {
    var that = this
    that.setData({ price: e.detail.value.replace(/[^\d.]/g, '') })
    that.setData({ price: e.detail.value.replace(/\.{2,}/g, '.') })
    that.setData({
      price: e.detail.value
        .replace('.', '$#$')
        .replace(/\./g, '')
        .replace('$#$', '.')
    })
    that.setData({
      price: e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
    })
  },

  bindFormSubmit: function() {
    var that = this
    var price = that.data.price
    var weightOne = that.data.weightOne
    var weightTwo = that.data.weightTwo
    if (!that.data.categoryStatus) {
      wx.showToast({
        title: '回收类别必选',
        icon: 'none'
      })
      return false
    } else if (!price) {
      wx.showToast({
        title: '核定单价必填',
        icon: 'none'
      })
      return false
    } else if (!weightOne || !weightTwo) {
      wx.showToast({
        title: '扣重/扣点必填',
        icon: 'none'
      })
      return false
    } else {
      wx.showLoading({ title: '提交中' })
      that.setData({ buthidden: true })
      wx.request({
        method: 'POST',
        url: app.preUrl + 'updateOrderStatus',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          id: that.data.orderInfo.id,
          orderStatus: 7,
          parentId: that.data.categoryIdOne, //品类父类ID
          categoryId: that.data.categoryIdTwo, //品类ID
          offWeight: parseFloat(weightOne).toFixed(2), //扣重 保留两位小数 单位:公斤
          offPointsWeight: weightTwo, //扣重百分比 保留两位小数 20.34代表20.34%
          price: parseFloat(price).toFixed(2) //单价 保留两位小数 单位:元
        },
        success: function(res) {
          console.log(res)
          wx.hideLoading()
          if (res.data.code === 100) {
            wx.showToast({
              title: '定价成功',
              icon: 'success',
              duration: 1000
            })
            setTimeout(function() {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              confirmColor: '#56BDE0',
              showCancel: false
            })
            that.setData({ buthidden: false })
          }
        },
        fail: function(error) {
          console.log(error)
          that.setData({ buthidden: false })
        }
      })
    }
  }
})
